package com.wg.dome.chat;

import com.wg.core.net.OperationResponse;
import com.wg.dome.ClientPeer;
import com.wg.dome.operation.ChatRequest;
import com.wg.dome.operation.ChatResponse;
import com.wg.dome.operation.OperationCode;
import com.wg.core.net.SendParameters;

import java.io.Serializable;
import java.util.HashMap;

public class ChatChannel {
    private final String name;
    private final HashMap<Serializable, ClientPeer> peers = new HashMap<>(100);

    public ChatChannel(String name) {
        this.name = name;
    }

    public Serializable getName() {
        return name;
    }

    public void addPeer(ClientPeer peer){
        if (peer == null){
            return;
        }
        peers.put(peer.getConnectionId(), peer);
    }

    public void removePeer(ClientPeer peer){
        if (peer == null){
            return;
        }
        peers.remove(peer.getConnectionId());
    }

    public void sendChat(ChatRequest request, SendParameters sendParameters){
        ChatResponse chatResponse = new ChatResponse();
        chatResponse.setChatName(request.getChatName());
        chatResponse.setPlayerInfo(request.getPlayerInfo());
        chatResponse.setMsg(request.getMsg());
        for (ClientPeer peer : peers.values()) {
            peer.sendOperationResponse(new OperationResponse(OperationCode.SEND_CHAT, chatResponse), sendParameters);
        }
    }
}
